package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.CodeAutoInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.Process;
import com.qidianit.mes.pro.mapper.CodeAutoInfoMapper;
import com.qidianit.mes.pro.mapper.ProcessMapper;
import com.qidianit.mes.pro.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: YanJiRan
 * @Date: 2025/9/17 - 下午2:16
 */
@Service
public class ProcessServiceImpl implements ProcessService {
    @Autowired
    private ProcessMapper processMapper;
    @Autowired
    private CodeAutoInfoMapper codeAutoInfoMapper;

    @Value("${process_code_type}")
    private String processCodeType;

    @Override
    public Result<String> autoCode() {
        CodeAutoInfo codeAutoInfo = codeAutoInfoMapper.selectByType(processCodeType);
        // 放入前缀
        StringBuilder sb = new StringBuilder(codeAutoInfo.getPre());
        // 日期
        sb.append(new SimpleDateFormat("YYMMdd").format(new Date()));
        // 序列号
        Integer sequence = codeAutoInfo.getSequence();
        // 在序列号前面补0
        for (int i = 0;i < 4 - sequence.toString().length();i++){
            sb.append("0");
        }
        sb.append(sequence.toString());
        // 将序列号+1，再保存回去
        codeAutoInfo.setSequence(codeAutoInfo.getSequence() + 1);
        codeAutoInfoMapper.update(codeAutoInfo);
        return Result.success(sb.toString());
    }

    @Override
    public Result<PageInfo<Process>> selectList(Process process, int page, int size) {
        //开启分页
        PageHelper.startPage(page,size);
        List<Process> processes = processMapper.selectList(process);
        return Result.success(new PageInfo<>(processes));
    }

    @Override
    public Result<Process> saveProcess(Process process) {
        Process process1 = processMapper.selectByCode(process.getProcessCode());
        if (process1 != null) {
            return Result.error("工序编码已存在，请更换编码后重试");
        }
        return processMapper.saveProcess(process) == 1 ? Result.success(process) : Result.error("操作失败");
    }

    @Override
    public Result<Process> selectByCode(String ProcessCode) {
        return Result.success(processMapper.selectByCode(ProcessCode));
    }

    @Override
    public Result updateById(Process process) {
        //先根据编码查询是否存在相同编码的工序
        Process process1 = processMapper.selectByCode(process.getProcessCode());
        //如果存在相同编码的工序
        if (process1 != null) {
            //检查id是否相同，不同则说明编码重复
            if (!process1.getId().equals(process.getId())) {
                return Result.error("工序编码已存在，请更换编码后重试");
            }
        }
        return processMapper.updateById(process) == 1 ? Result.success() : Result.error("操作失败");
    }

    @Override
    public Result deleteById(Long id) {
        return processMapper.deleteById(id) == 1 ? Result.success() : Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return processMapper.batchDelete(ids) > 0 ? Result.success() : Result.error("操作失败");
    }
}
